package com.rt.schedulenew.web;

import com.rt.schedulebase.dto.DictUsersDto;
import com.rt.schedulenew.dto.RemoveCacheRequest;
import com.rt.schedulenew.dto.utils.Image2BarcodeImageDto;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.base.ShiroUtil;
import com.rt.schedulenew.utils.global.ErrorCode;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.util.EhCacheUtil;
import com.rt.schedulenew.utils.util.QrCodeUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping({"/utils"})
public class UtilsController {

    /**
     * 将文字内容转成base64图片
     *
     * @param dto
     * @return
     */
    @PostMapping({"/toBarBase64"})

    public JsonResult Str2BarcodeImage(@RequestBody Image2BarcodeImageDto dto) {
        String base64Str = QrCodeUtil.creatBarCode(dto.getContent(), dto.getWidth(), dto.getHeight());

        return JsonResultUtil.success(ErrorCode.SUCCESS.getValue(), base64Str);
    }

    /**
     * 将文字内容转成base64图片
     *
     * @param dto
     * @return
     */
    @PostMapping({"/removeCache"})
    public JsonResult removeCache(@RequestBody RemoveCacheRequest dto) {
        DictUsersDto currentUser = (DictUsersDto) ShiroUtil.getCurrentUser();
        if (Objects.nonNull(currentUser) && "patient".equals(currentUser.getRoleId())) {
            return JsonResultUtil.failure("只能管理员调用该接口!");
        }
        EhCacheUtil.remove(dto.getCacheName(), dto.getCacheKey());
        return JsonResultUtil.success();
    }
}
